package com.sansui.admin.core.service.impl;

import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sansui.admin.common.model.Result;
import com.sansui.admin.core.dao.UserMapper;
import com.sansui.admin.core.dao.UserRoleMapper;
import com.sansui.admin.core.model.User;
import com.sansui.admin.core.model.UserAddModel;
import com.sansui.admin.core.model.UserPage;
import com.sansui.admin.core.model.UserRole;
import com.sansui.admin.core.service.UserService;
import com.sansui.admin.core.vo.UserVO;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserRoleMapper userRoleMapper;

	@Override
	public Result insertUser(UserAddModel user) {
		String[] roleId = user.getRoleId();
		user.setPassword(DigestUtils.md5Hex(user.getPassword()));
		user.setId(UUID.randomUUID().toString());
		userMapper.insert(new User(user.getId(), user.getUsername(), user.getPassword()));
		if (roleId == null) {
			return new Result("success", null);
		}
		for (int i = 0; i < roleId.length; i++) {
			userRoleMapper.insert(new UserRole(UUID.randomUUID().toString(), user.getId(), roleId[i]));
		}
		return new Result("success", null);
	}

	@Override
	public Result updateUser(UserAddModel user) {
		String[] roleId = user.getRoleId();
		if (!"".equals(user.getPassword())) {
			user.setPassword(DigestUtils.md5Hex(user.getPassword()));
			userMapper.updateByPrimaryKeySelective(new User(user.getId(), user.getUsername(), user.getPassword()));
		}
		userMapper.updateByPrimaryKeySelective(new User(user.getId(), user.getUsername()));

		userRoleMapper.deleteByT(new UserRole[] { new UserRole(null, user.getId(), null) });

		if (roleId == null) {
			return new Result("success", null);
		}
		for (int i = 0; i < roleId.length; i++) {
			userRoleMapper.insert(new UserRole(UUID.randomUUID().toString(), user.getId(), roleId[i]));
		}
		return new Result("success", null);
	}

	@Override
	public Result deleteUser(String[] id) {
		UserRole[] u = new UserRole[id.length];
		for (int i = 0; i < id.length; i++) {
			u[i] = new UserRole(null, id[i], null);
		}
		userRoleMapper.deleteByT(u);
		userMapper.deleteBy(id);

		return new Result("success", null);
	}

	@Override
	public User selectByPrimaryKey() {
		User u = userMapper.selectByPrimaryKey((String) SecurityUtils.getSubject().getPrincipal());
		u.setPassword("");
		return u;
	}

	@Override
	public Result updatePassword(UserAddModel user) {
		if (!"".equals(user.getPassword())) {
			user.setPassword(DigestUtils.md5Hex(user.getPassword()));
			userMapper.updateByPrimaryKeySelective(new User(user.getId(), user.getUsername(), user.getPassword()));
		}
		userMapper.updateByPrimaryKeySelective(new User(user.getId(), user.getUsername()));
		return new Result("success", null);
	}

	@Override
	public UserVO selectByPrimaryKey(String id) {
		UserVO u = userMapper.selectVOByPrimaryKey(id);
		u.setPassword("");
		u.setUserRoleList(userRoleMapper.select(new UserRole(null, id, null)));
		return u;
	}

	@Override
	public User findTByT(User user) {
		return userMapper.selectOne(user);
	}

	@Override
	public void deleteByT(UserRole[] u) {
		userRoleMapper.deleteByT(u);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * <p>Title: findTByPage</p>
	 * 
	 * <p>Description: </p>
	 * 
	 * @param user
	 * 
	 * @return
	 * 
	 * @see
	 * com.sansui.admin.core.service.UserService#findTByPage(com.sansui.admin.
	 * core.model.UserPage)
	 * 
	 */
	@Override
	public Object findTByPage(UserPage user) {
		// TODO Auto-generated method stub
		return this.userMapper.selectByExample(user);
	}

}
